package com.kgame5.xtank3d.model.cart.fsms.move {
import kgame5.kgu.spacial.SpacialManager;
import kgame5.kmath.MathDG;
import kgame5.kutil.SafeTimer;

import com.kgame5.xtank3d.GameEnum;
import com.kgame5.xtank3d.model.tank.fsms.TankState;
import com.kgame5.xtank3d.vo.MyTankVO;

/*
 * 注意v要设置值，这样才能通信
 */
public class WanderMovState extends TankState{
	
	
	//__________________Propetty__________________
	
	//______________________Method___________________
	public  function WanderMovState(vo:MyTankVO){
		super(GameEnum.STATE_EM_WANDER_MOVE, vo);
		
	}
	
	//进入状态
	override public function enter(info:Object=null):void{
		//T.p("进入漫游");
		timer.start(MathDG.random(tankVO.ai.mov_wander_dur_min, tankVO.ai.mov_wander_dur_max));
			
		turnHead();
	}

	//执行状态
	override public function update():void{
		
		if(timer.isOK()){//时间到进入思考状态思考
			fsm.SwitchState(GameEnum.STATE_EM_THINK_MOVE, null);
		}else{//每3秒钟，检测一下是否撞上了障碍物
			var isCollide:Boolean=tankMove();
			if(isCollide){//撞上了东西
				//fsm.SwitchState(AppEnum.STATE_EM_THINK_MOVE, null);
				turnHead();//调头
			}
		}
	}
	
	//退出状态
	override public function exit():void{
		
	}
	 
	 //测试，此方向的一格距离以后，是否撞上了障碍物
	private function tankMove():Boolean{
		return spacialManager.ProcessSimpleMove4(tankVO.hardVO);
	}
	 //执行一下调头，找到一个可移动的方向
	private function turnHead():void{
		var aOpenDir:Array=spacialManager.gc.getOpenDirs4(tankVO.hardVO);
		var idx:int=MathDG.random(0, aOpenDir.length-1);
		var dir:int=aOpenDir[idx];
		//T.p("选定方向为:"+dir);
		switch(dir){
		case SpacialManager.DIR_UP:
			v.x=0;
			v.z=tankVO.body.MOV;
			r.y=0;
			break;
		case SpacialManager.DIR_DOWN:
			v.x=0;
			v.z=-tankVO.body.MOV;
			r.y=180;
			break;
		case SpacialManager.DIR_LEFT:
			v.x=-tankVO.body.MOV;
			v.z=0;
			r.y=270;
			break;
		case SpacialManager.DIR_RIGHT:
			v.x=tankVO.body.MOV;
			v.z=0;
			r.y=90;
			break;
		}
	}
}//end of class
}
